package com.dmm.user.controller;


import com.dmm.result.Result;
import com.dmm.user.entity.User;
import com.dmm.user.service.UserService;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * <p>
 * 用户信息 前端控制器
 * </p>
 *
 * @author dmm
 * @since 2022-10-13
 */
@Api(tags = "用户模块")
@RestController
@CrossOrigin
@RequestMapping("/user/login")
public class UserController {

    @Resource
    UserService userService;

    //查询用户id的信息
    @GetMapping("FindAll/{id}")
    public Result FindAll(@PathVariable Integer id){

        User user=userService.FindAll(id);
        return Result.success(user);

    }


    //登录
    @PostMapping("/Sign-in")
    public Result Sign_in(@RequestParam("email") String email,
                          @RequestParam("password")String password,
                          HttpServletRequest request){
        //查询该用户
        User user=userService.getemail(email,password);

        if(user==null){
            return Result.success(null).message("登陆失败");
        }
        //对Session进行存储
        HttpSession session = request.getSession();
        session.setAttribute("email",user);
        return Result.success(null).message("登陆成功");

    }

    //校验是否登录

    @GetMapping("check")
    public Result check(HttpServletRequest request){

        HttpSession session = request.getSession();
        User user =(User) session.getAttribute("email");
        if(user==null){
            return Result.success(null).message("没有登录");
        }
        return Result.success(user);
    }

    //退出
    @GetMapping("sign-out")
    public Result sign_out(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("email");
        return Result.success(null);
    }

    //注册 ;
    @PostMapping("/register")
    public Result register(@RequestBody User user,
                           @RequestParam String code){

        Boolean bool=userService.register(user,code);

        if(bool){
            return Result.success(null).message("注册成功");
        }
        return Result.success(null).message("注册失败");
    }


    //验证码获取
    @ApiOperation("验证码获取")
    @PostMapping("/VerificationCode")
    public Result Verification_Code(@RequestParam String email){

        userService.getCode(email);

        return Result.success(null);
    }




}

